{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Part 2, Topic 1, Lab A: Instruction Power Differences - SIMULATED Setup"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "**THIS IS NOT THE COMPLETE TUTORIAL - see file with `(MAIN)` in the name.**\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sure you don't have hardware, but that doesn't mean we can't have fun! If you check the ChipWhisperer based lab (using hardware), you'll find that the capture function is defined like this:\n",
    "    \n",
    "```python\n",
    "def capture_trace():\n",
    "    ktp = cw.ktp.Basic()\n",
    "    key, text = ktp.next()\n",
    "    return cw.capture_trace(scope, target, text).wave\n",
    "```\n",
    "Which sends a simpleserial command to the target and captures a power trace. For the hardware, it's up to you to edit the firmware, build it, and upload it before capturing each trace. We can't do that, so we need to load some trace data and mimic the `capture_trace()` calls. You can use it in mostly the same way, but one caveat is that which trace is returned changes with each call. You can select a specific trace with the `idx` parameter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prerec_traces_idx = 0\n",
    "def capture_trace(idx=None):\n",
    "    global prerec_traces_idx\n",
    "    if idx is None:\n",
    "        if prerec_traces_idx >= (len(prerec_traces)):\n",
    "            prerec_traces_idx = 0\n",
    "        prerec_traces_idx += 1\n",
    "        print(prerec_traces_idx)\n",
    "        return prerec_traces[prerec_traces_idx-1]  \n",
    "    else:\n",
    "        return prerec_traces[idx]\n",
    "    \n",
    "#load traces\n",
    "import numpy as np\n",
    "prerec_traces = []\n",
    "for i in range(4):\n",
    "    prerec_traces.append(np.load(\"traces/lab2_1A_trace{}.npy\".format(i)))\n",
    "    \n",
    "assert(len(prerec_traces[0]) == 2000)\n",
    "print(\"✔️ OK to continue!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But wait - this lab isn't the one you need to run it in! Instead copy the above block into the lab in the requested section, and you should be ready to rock."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
